package com.googlecode.imule.neilisageniuswhoshouldgetanA;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.interpolation.SplineInterpolator;
import org.jdesktop.animation.transitions.ComponentState;
import org.jdesktop.animation.transitions.ScreenTransition;
import org.jdesktop.animation.transitions.TransitionTarget;
import org.jdesktop.swingx.JXButton;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXPanel;

/**
 * An simple iPhone-like menu system for the application
 * 
 * @author Neil Cowzer
 */
/* TODO: finalise implementation and Document
 * Modify to use the tablelayout, keep at top and not to expand icon/buttons
 * Make it just the panel handling itself, need a another class to control the
 * 'context/app' switch.
 * implement explode and implode.
 */
public class iMenu extends JXPanel implements TransitionTarget{
	
	private LayoutManager layout;
	
	private List<iButtonPosition> list;	// list of buttons 
	
	private int count;	// number of buttons
	
	private JXPanel contentPanel, buttonPanel;	// temp panels
	
	private ScreenTransition st;
	
	public iMenu(){
		super();
		initComponent();
		
        // -- create the animator object (set delay and interpolator)
        Animator anim = new Animator(600);
        // http://javadesktop.org/swinglabs/demos/timingframework/SplineEditor.jnlp
        anim.setInterpolator(new SplineInterpolator(
                0.00f, 1.00f,
                1.00f, 0.98f));

        // -- setup the screen transition object, with TransitionTarget & Animator
        st = new ScreenTransition(
                contentPanel,
                this,
                anim
        );
	}
	
	public void add(iMenuButton button){
		iButtonPosition toAdd = new iButtonPosition(button, count/3, count%3);
		
		list.add(toAdd);
		
		sort();
		
		count++;
	}
	
	private void sort(){
		Collections.sort(list);
		
		int i = 0;
		for (iButtonPosition data : list) {
			int x = data.x;
			int y = data.y;
			/*if (x == i/3 || y == i%3){
				// correct
			} else {*/
				//contentPanel.add(data.button, ""+x+", "+y+"");
				contentPanel.add(data.button);
				data.x = x;
				data.y = y;
			//}
			i++;
		}
	}
	
	
	public void setupNextScreen() {
		contentPanel.removeAll();
		contentPanel.setLayout(layout);
		
			
		if (count%2 == 0)
			add(new iMenuButton("/resources/Music.png", ""+0));
		else
			add(new iMenuButton("/resources/Mail.png", ""+count));
		
		
		
		contentPanel.revalidate();
		contentPanel.repaint();
	}
	
	
	private void setupOffScreen(){
		contentPanel.setLayout(null);
		
		//contentPanel.add(new JXLabel(""));
		
		iMenuButton button = new iMenuButton("/resources/Mail.png", "0");
		list.add(new iButtonPosition(button, 0, 0));
		contentPanel.add(button);
		Dimension size = button.getPreferredSize();
		button.setBounds(700, 20, size.width, size.height);
		
		iMenuButton button1 = new iMenuButton("/resources/Mail.png", "1");
		list.add(new iButtonPosition(button1, 50, 50));
		contentPanel.add(button1);
		size = button1.getPreferredSize();
		button1.setBounds(-200, -200, size.width, size.height);
	}
	
	
	
	/**
	 * Instantiate the components
	 */
	private void initComponent(){
		/*double f = TableLayout.FILL;
		
		double[][] size = {
				{f, f, f}, 
				{f, f, f, f}};
		layout = new TableLayout(size);*/
		layout = new GridLayout(0,3);
		
		JXButton button = new JXButton("Emplode");
		button.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				transition(e);
			}
		});
		buttonPanel = new JXPanel();
		buttonPanel.add(button);
		
		contentPanel = new JXPanel();
		contentPanel.setLayout(layout);
		contentPanel.setBackground(Color.BLACK);
		
		setLayout(new BorderLayout());
		add(contentPanel, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);
		
		list = new ArrayList<iButtonPosition>();
		count = 0;
		
		setupOffScreen();
	}
	
	private void transition(ActionEvent e){
		st.start();
	}
	
	
	protected class iButtonPosition implements Comparable<iButtonPosition>{
		
		protected iMenuButton button;
		protected int x,y;
		
		public iButtonPosition(iMenuButton button, int x, int y) {
			this.button = button;
			this.x = x;
			this.y = y;
		}

		public int compareTo(iButtonPosition arg0) {
			return button.getName().compareTo(arg0.button.getName());
		}
	}
}
